package com.sunwayworld.dev;

import java.sql.SQLException;

import com.sunwayworld.dev.core.generate.GenerateEJB2XML;
import com.sunwayworld.dev.core.generate.GenerateHibernateXML;
import com.sunwayworld.dev.core.generate.GenerateJAVA;
import com.sunwayworld.dev.core.generate.GenerateJSP;
import com.sunwayworld.dev.core.valueobject.JavaConst;
import com.sunwayworld.dev.core.valueobject.JavaObject;
import com.sunwayworld.dev.core.valueobject.JspObject;
import com.sunwayworld.dev.util.StringDevUtil;

/**
 * DevTools 程序的入口
 * 
 * @author sagitta
 * 
 */
public class DevTools {

	private static final DevTools devTools = new DevTools();

	/**
	 * @param args
	 */
//	public static void main(String[] args) {
//		String temp="";
//		while(!temp.equals("N")){
//			temp="";
//		String packageName=devTools.printConsoleLine("Please Enter the model's package");
//		
//		String tableName=devTools.printConsoleLine("Please Enter the table's name");
//		System.out.println("Start Generate Code");
//		
//		try {
//			devTools.generateEJB2(StringDevUtil.toString(packageName), StringDevUtil.toString(tableName));
//			
//		} catch (Exception e) {
//			System.out.println(" Generate Code Error\n\n");
//			e.printStackTrace();
//			System.out.println(" \n\n");
//		}
//		
//		
//		temp=devTools.printConsoleLine("Are You go on ? 'Y' or 'N'");
//		}
//		
//	}
	
	public static void main(String[] args) {
		try {
			System.out.println("Start Generate Code");
			devTools.generateEJB2("com.sunwayworld.srm30.kcckd", "KCCKD");
			System.out.println("Generate Code Success");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
//	@SuppressWarnings("unused")
//	private String printConsoleLine(String message)
//    {
//        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//        String str = null;
//        System.out.println(message);
//        try {
//			str = br.readLine();
//			return str;
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//			return "";
//		}
//    }


	public void generateEJB2(String packageName, String tableName) throws Exception {

		String modueName = StringDevUtil.convertTableName(tableName);

		GenerateJAVA g = null;

		JavaObject valueobject = new JavaObject(packageName + ".valueobject.V"
				+ modueName + "Info", "/vm/单表/java/ejb2.0/valueobject.vm", tableName);
		g = new GenerateJAVA(valueobject);
		g.generateCode();
		
		

		//////////////////////////////////////////////////////
		JavaObject entity = new JavaObject(packageName + ".entity.E"
				+ modueName , "/vm/单表/java/ejb2.0/Entity.vm");
		g = new GenerateJAVA(entity);
		entity.putContext(JavaConst.valueobject, valueobject);
		g.generateCode();

		JavaObject entityHome = new JavaObject(packageName + ".entity.E"
				+ modueName+"Home" , "/vm/单表/java/ejb2.0/EntityHome.vm");
		g = new GenerateJAVA(entityHome);
		entityHome.putContext(JavaConst.valueobject, valueobject);
		entityHome.putContext(JavaConst.Interface,entity);
		g.generateCode();		

		
		JavaObject entityBean = new JavaObject(packageName + ".entity.E"
				+ modueName+"Bean" , "/vm/单表/java/ejb2.0/EntityBean.vm");
		g = new GenerateJAVA(entityBean);
		entityBean.putContext(JavaConst.valueobject, valueobject);
		g.generateCode();	
		
		
		
		JavaObject session = new JavaObject(packageName + ".session.S"
				+ modueName , "/vm/单表/java/ejb2.0/Session.vm");
		g = new GenerateJAVA(session);
		session.putContext(JavaConst.valueobject, valueobject);
		session.putContext(JavaConst.Interface,entity);
		g.generateCode();

		JavaObject sessionHome = new JavaObject(packageName + ".session.S"
				+ modueName+"Home" , "/vm/单表/java/ejb2.0/SessionHome.vm");
		g = new GenerateJAVA(sessionHome);
		sessionHome.putContext(JavaConst.valueobject, valueobject);
		sessionHome.putContext(JavaConst.Interface,session);
		g.generateCode();		

		
		JavaObject sessionBean = new JavaObject(packageName + ".session.S"
				+ modueName+"Bean" , "/vm/单表/java/ejb2.0/SessionBean.vm");
		g = new GenerateJAVA(sessionBean);
		sessionBean.putContext(JavaConst.valueobject, valueobject);
		sessionBean.putContext(JavaConst.Interface,entity);		
		g.generateCode();	
		

		JavaObject proxy = new JavaObject(packageName + ".web.proxy."
				+ modueName+"Proxy" , "/vm/单表/java/ejb2.0/Proxy.vm");
		g = new GenerateJAVA(proxy);
		proxy.putContext(JavaConst.valueobject, valueobject);
		proxy.putContext(JavaConst.Interface, session);
		g.generateCode();	

		

		JavaObject action = new JavaObject(packageName + ".web.action."
				+ modueName + "Action", "/vm/单表/java/ejb2.0/Action.vm");
		action.description = "";

		action.putContext(JavaConst.valueobject, valueobject);
		action.putContext(JavaConst.Interface, proxy);
		
		g = new GenerateJAVA(action);
		g.generateCode();
		
		//
		
		
		
		
		
		GenerateEJB2XML gxml=new GenerateEJB2XML("/vm/单表/java/ejb2.0",modueName,tableName);
//		gxml.putContext(JavaConst.entity,entity);
//		gxml.putContext(JavaConst.session,session);
		gxml.generateCode();
		
		
		
		

	}
	
	
	
	public void generateHibernate(String packageName, String tableName) throws Exception {

		String modueName = StringDevUtil.convertTableName(tableName);

		GenerateJAVA g = null;

		JavaObject valueobject = new JavaObject(packageName + ".valueobject.V"
				+ modueName + "Info", "/vm/单表/java/valueobject.vm", tableName);
		g = new GenerateJAVA(valueobject);

		g.generateCode();
		valueobject.setVmPath("/vm/单表/java/hibernate.hbm.xml.vm");

		GenerateHibernateXML gxml = new GenerateHibernateXML(valueobject);
		gxml.generateCode();

		JavaObject service = new JavaObject(packageName + ".service."
				+ modueName + "Manager", "/vm/单表/java/Manager.vm", null);
		service.description = "接口";
		service.putContext(JavaConst.valueobject, valueobject);

		g = new GenerateJAVA(service);
		g.generateCode();

		JavaObject serviceImpl = new JavaObject(packageName + ".service."
				+ modueName + "ManagerImpl", "/vm/单表/java/ManagerImpl.vm", null);
		serviceImpl.description = "接口的实现";

		serviceImpl.putContext(JavaConst.valueobject, valueobject);
		serviceImpl.putContext(JavaConst.Interface, service);

		g = new GenerateJAVA(serviceImpl);
		g.generateCode();

		JavaObject action = new JavaObject(packageName + ".web.action."
				+ modueName + "Action", "/vm/单表/java/Action.vm");
		action.description = "";
		action.putContext(JavaConst.Interface, service);
		action.putContext(JavaConst.InterfaceImpl, serviceImpl);
		action.putContext(JavaConst.valueobject, valueobject);

		
		
		g = new GenerateJAVA(action);
		g.generateCode();

	}

	public void generateJsp() throws SQLException {
		GenerateJSP g = null;
		JavaObject valueobject = new JavaObject(
				"com.sunwayworld.sundown.xtuser.valueobject.VXtjlxtyh",
				"/vm/单表/java/valueobject.vm", "t_temp");
		// /////////////////////////////////////////////////////////

		JspObject list = new JspObject("WEB-INF/jsp/xtuser/xtuserList.jsp",
				"/vm/单表/jsp/list.vm");
		list.description = "";

		list.putContext(JavaConst.valueobject, valueobject);

		g = new GenerateJSP(list);
		g.generateCode();

		JspObject detail = new JspObject("WEB-INF/jsp/users/xtuserDetail.jsp",
				"/vm/单表/jsp/detail.vm");
		detail.description = "";

		detail.putContext(JavaConst.valueobject, valueobject);

		g = new GenerateJSP(detail);
		g.generateCode();

	}

}
